package com.yupi.project.security;

import com.yupi.project.model.AuthenticatorResult;
import com.yupi.project.model.ProtectedRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 责任链模式校验请求
 * @author SummerBird
 * @date 2024/3/8 18:02
 * @version 1.0
 */
@Component
@Slf4j
public class SecurityVerificationChain {
    @Resource
    private List<SecurityVerificationHandler> securityVerificationHandlers;

    public AuthenticatorResult handler(ProtectedRequest protectedRequest){
        for (SecurityVerificationHandler securityVerificationHandler : securityVerificationHandlers) {
            AuthenticatorResult result = securityVerificationHandler.auth(protectedRequest);
            // 任何一个校验不通过，就返回失败
            if(!result.isSuccess()){
                return result;
            }
        }
        return AuthenticatorResult.success();
    }
}
